/**
 * @file 实现一个Mac地址校验类
 * @date 2023-6-2 19:56:14
 *
 * var mac = new Mac('-'); // 注意分隔符长度不限，可以是'::'
 * mac.set('11-22-33-44-55-66'); // 格式不对返回false
 * mac == '11-22-33-44-55-66'; // true
 */

class Mac {
  delimiter = "";
  #val = "";
  #REG = /[0-9a-fA-F]{2}/;

  constructor(delimiter) {
    this.delimiter = delimiter;
  }

  set(val) {
    this.#buildReg();
    if (this.#REG.test(val)) {
      this.#val = val;
    } else {
      return false;
    }
  }

  #buildReg() {
    const char = "[0-9a-fA-F]{2}";
    this.#REG = new RegExp(Array(6).fill(char).join(this.delimiter));
  }

  [Symbol.toPrimitive]() {
    return this.#val;
  }
}

var mac = new Mac("-");
mac.set("11-22-33-44-55-66");
mac == "11-22-33-44-55-66"; // true
